package com.banshuibao.bsb_android.activity;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.UserMessageModel;
import com.banshuibao.bsb_android.util.PriceUtil;
import com.banshuibao.bsb_android.util.XToast;
import com.banshuibao.bsb_android.wxapi.WXEntryActivity;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WithdrawalsActivity extends BaseActivity implements View.OnClickListener {

    private EditText edit;
    private TextView money, all;
    private Button btn;
    private double balance;

    private IWXAPI api;

    LocalBroadcastManager localBroadcastManager;
    BroadcastReceiver bReceiver;
    IntentFilter iFilter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdrawals);
        api = WXAPIFactory.createWXAPI(this, Constants.WXID.APP_ID);
        api.registerApp(Constants.WXID.APP_ID);
        iFilter = new IntentFilter();
        iFilter.addAction("myAction");
        iFilter.setPriority(100);
        bReceiver = new WXEntryActivity.TixianBroad();
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        localBroadcastManager.registerReceiver(bReceiver, iFilter);
        initView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(bReceiver);
    }

    private void initView() {
        setTitleText("提现");
        edit = findViewById(R.id.edit);
        money = findViewById(R.id.money);
        all = findViewById(R.id.all);
        btn = findViewById(R.id.tx_btn);
        all.setOnClickListener(this);
        btn.setOnClickListener(this);
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 0) {
                    btn.setBackground(getResources().getDrawable(R.drawable.tx_shape));
                    btn.setClickable(true);
                } else {
                    btn.setBackground(getResources().getDrawable(R.drawable.tx_shape_two));
                    btn.setClickable(false);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.all:
                if (balance > 0) {
                    edit.setText(PriceUtil.FormatDecimal(balance));
                    edit.setSelection(edit.getText().length());
                    btn.setClickable(true);
                    btn.setBackground(getResources().getDrawable(R.drawable.tx_shape));
                } else {
                    btn.setClickable(false);
                    XToast.showToast(this, "当前可用余额为0");
                }
                break;
            case R.id.tx_btn:
                String money = edit.getText().toString().trim();
                double v = Double.parseDouble(money);
                if (!isWXAppInstalledAndSupported()) {
                    XToast.showToast(this, "请安装或更新微信最新版本客户端");
                    return;
                }
                if (TextUtils.isEmpty(money)) {
                    btn.setClickable(false);
                    XToast.showToast(this, "请输入提现的金额");
                    return;
                }
                if (v > balance) {
                    XToast.showToast(this, "提现金额不能大于您的余额");
                    return;
                }
                if (v < 10) {
                    XToast.showToast(this, "提现金额最低需要10元");
                    return;
                }
                setRequest();
                Intent intent = new Intent();
                intent.setAction("myAction");
                intent.putExtra("money", money);
                localBroadcastManager.sendBroadcastSync(intent);
//                finish();
                break;
        }
    }

    private void setRequest() {
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_sdk_demo_test";
        api.sendReq(req);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getWallet();
    }

    //读取用户信息得到余额
    public void getWallet() {
        HttpUtil.doPost(Constants.Url.USER_MESSAGE, null, new HttpResponse(this, UserMessageModel.class) {
            @Override
            public void onResult(Object obj) {
                UserMessageModel messageModel = (UserMessageModel) obj;
                updateUserMessage(messageModel.getData());
            }
        });
    }

    void updateUserMessage(UserMessageModel.Data user) {
        balance = user.getBalance();
        String price = PriceUtil.FormatDecimal(balance);
        this.money.setText("可用余额" + price);
    }

    //判断是否安装微信客户端
    private boolean isWXAppInstalledAndSupported() {
        boolean sIsWXAppInstalledAndSupported = api.isWXAppInstalled();
        return sIsWXAppInstalledAndSupported;
    }
}
